package cn.anlab.anappframe;

import java.util.Locale;

import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.widget.Toast;
import cn.anlab.anappframe.system.CoreApplication;
import cn.anlab.anappframe.system.Event;
import cn.anlab.anappframe.widget.CompProgressDialog;

public class BaseActivity extends Activity {
	
	 
	private CompProgressDialog loadingDialog;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		interest(errorHandler, Event.EXCEPTION);
	}
	
	/**
	 * 注册当前页面（Activity）感兴趣的事件
	 * @param handler 捕获事件下由handler来处理
	 * @param event
	 * @see Event
	 */
	protected void interest(Handler handler, int ... events) {
		for(int e : events) {
			CoreApplication.registerHandler(e, handler);
		}
	}
	
	
	/**
	 * 显示加载中圆圈
	 */
	protected void showLoading() {
		if(loadingDialog == null) {
			loadingDialog = new CompProgressDialog(this);
		}
		if(!loadingDialog.isShowing()) {
			loadingDialog.show();			
		}
	}
	
	/**
	 * 隐藏加载中圆圈
	 */
	protected void hideLoading() {
		if(loadingDialog != null) {
			loadingDialog.dismiss();
		}
	}
	
	
	
	/**
	 * 统一错误处理，用于所有页面
	 */
	private Handler errorHandler = new Handler() {
		public void handleMessage(Message msg) {
			hideLoading();
			Toast.makeText(BaseActivity.this, "aaaa", Toast.LENGTH_SHORT).show();
		};
	};
	

}
